#include "cximessagebox_p.h"
#include <QAbstractButton>
#include <QApplication>
#include <QClipboard>
#include <QStyle>
#include <QStyleOptionTitleBar>
#include <common/localmachine/localmachine.h>
#include <ui_CxiMessageBox.h>
CxiMessageBoxPrivate::CxiMessageBoxPrivate(CxiMessageBox* p)
	: QObject(p),
	  q_ptr(p)
{
}
CxiMessageBoxPrivate::~CxiMessageBoxPrivate()
{
	delete ui;// 释放UI指针
}
void CxiMessageBoxPrivate::init()
{
	Q_Q(CxiMessageBox);
	// 初始化UI
	ui = new Ui::CxiMessageBox;
	ui->setupUi(q);

	// 设置默认窗口标志
	if (useDefaultWindowFlags)
	{
		q->setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowCloseButtonHint);
	}

	// 同步模态
	q->setModal(model);

	// 执行原公开类的_init逻辑
	_init();

	// 连接信号槽（槽函数是Private的成员）
	connect(ui->buttonBox, &QDialogButtonBox::clicked, this, &CxiMessageBoxPrivate::onButtonClicked);
	connect(ui->copyButton, &QPushButton::clicked, this, &CxiMessageBoxPrivate::copyButtonClicked);
}
void CxiMessageBoxPrivate::_init()
{
	Q_Q(CxiMessageBox);
	// 设置提示文本和窗口标题
	ui->textLabel->setText(text);
	q->setWindowTitle(title);
	// 加载图标
	loadIcon(icon);
	// 默认按钮组（无按钮时显示OK）
	if (buttons == CxiMessageBox::NoButton)
	{
		buttons = CxiMessageBox::Ok;
	}
	// 设置复制按钮图标
	QIcon copyIcon = QIcon::fromTheme("edit-copy", q->style()->standardIcon(QStyle::SP_FileIcon));
	ui->copyButton->setIcon(copyIcon);
	// 设置按钮组
	ui->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons)));
}
void CxiMessageBoxPrivate::loadIcon(CxiMessageBox::Icon icon)
{
	Q_Q(CxiMessageBox);
	QStyle* style = QApplication::style();
	QPixmap pixmap;
	// 根据图标类型获取系统内置图标
	switch (icon)
	{
	case CxiMessageBox::Information:
		pixmap = style->standardIcon(QStyle::SP_MessageBoxInformation).pixmap(ICON_SIZE);
		break;
	case CxiMessageBox::Question:
		pixmap = style->standardIcon(QStyle::SP_MessageBoxQuestion).pixmap(ICON_SIZE);
		break;
	case CxiMessageBox::Success:
		pixmap = style->standardIcon(QStyle::SP_MessageBoxInformation).pixmap(ICON_SIZE);
		break;
	case CxiMessageBox::Warning:
		pixmap = style->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(ICON_SIZE);
		break;
	case CxiMessageBox::Critical:
		pixmap = style->standardIcon(QStyle::SP_MessageBoxCritical).pixmap(ICON_SIZE);
		break;
	default:
		ui->iconLabel->setVisible(false);
		return;
	}
	ui->iconLabel->setPixmap(pixmap);
	ui->iconLabel->setVisible(true);
}

void CxiMessageBoxPrivate::updateSize()
{
	Q_Q(CxiMessageBox);
	// 计算标题宽度，调整窗口大小以适配标题
	QStyleOptionTitleBar option;
	option.initFrom(q);
	option.text = q->windowTitle();
	option.titleBarState = q->windowState();
	option.titleBarFlags = q->windowFlags();

	QFontMetrics fontMetrics = option.fontMetrics;
	int titlePixelWidth = fontMetrics.horizontalAdvance(option.text);
	QSize originalSize = q->size();

	// 若标题过宽，扩展窗口宽度
	if (titlePixelWidth >= originalSize.width() / 2)
	{
		int newWidth = originalSize.width() + (titlePixelWidth - originalSize.width() / 2);
		QSize newSize(newWidth, originalSize.height());
		newSize = q->frameSize().expandedTo(newSize);
		q->resize(newSize);
	}
	else
	{
		q->resize(q->frameSize().expandedTo(originalSize));
	}
}

void CxiMessageBoxPrivate::setIPTitle()
{
	Q_Q(CxiMessageBox);
	if (showAsIPTitle)
	{
		// 拼接IP到标题（原公开类的逻辑迁移）
		QString ipTitle = QString("%1 - %2 - %3")
								  .arg(q->windowTitle(),
									   QString::fromStdString(LocalMachine::getHostName()),
									   QString::fromStdString(LocalMachine::getIpAddress()));
		q->setWindowTitle(ipTitle);
	}
}

void CxiMessageBoxPrivate::onButtonClicked(QAbstractButton* button)
{
	Q_Q(CxiMessageBox);
	// 记录点击的按钮
	clickedButton = CxiMessageBox::StandardButton(int(ui->buttonBox->standardButton(button)));

	// 根据按钮角色处理对话框关闭逻辑
	switch (ui->buttonBox->buttonRole(button))
	{
	case QDialogButtonBox::AcceptRole:
	case QDialogButtonBox::YesRole:
		q->accept();
		break;
	case QDialogButtonBox::RejectRole:
	case QDialogButtonBox::NoRole:
		q->reject();
		break;
	default:
		q->close();
		break;
	}
}

void CxiMessageBoxPrivate::copyButtonClicked()
{
	Q_Q(CxiMessageBox);
	// 复制提示文本到剪贴板
	QApplication::clipboard()->setText(ui->textLabel->text());
}